package com.hoshiicloud.common.validate.annotations.impl;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.hoshiicloud.common.validate.annotations.IdCard;
import org.apache.commons.lang3.StringUtils;

/**
 * 身份证验证注解实现
 *
 * @author : yangcheng
 * @date : 2019/5/13 16:14
 * @mail : maiboyc@163.com
 **/
public class IdCardValidatorImpl  implements ConstraintValidator<IdCard, String> {

    @Override
    public void initialize(IdCard constraintAnnotation) {

    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if(StringUtils.isBlank(value)){
            return false;
        }
        String regex = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";
        if (value.matches(regex)) {
            return true;
        } else {
            return false;
        }
    }

}